package com.example.agendamesquita;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class EventoDataSource {
	
	private SQLiteDatabase db;
	private HelperDB helperDB;
	private String[] columns = {"_id", "nome", "datahora", "local", "descricao"};
	
	public EventoDataSource(Context context)
	{
		helperDB = new HelperDB(context);
	}
	
	public void open() throws SQLException
	{
		db = helperDB.getWritableDatabase();
	}
	
	public void close()
	{
		db.close();
	}

	public long addEvento(Evento e)
	{
		ContentValues values = new ContentValues();
		values.put("nome", e.getNome());
		values.put("datahora", e.getDiaHora());
		values.put("local", e.getLocal());
		values.put("descricao", e.getDescricao());
		
		long insertId = db.insert("Evento", null, values);
		return insertId;/*
		Cursor cursor = db.query("Evento", columns, "_id = " + insertId, null, null, null, null);
		
		cursor.moveToFirst();
		Evento newEvento = cursorToEvento(cursor);
		cursor.close();
		
		return newEvento;*/
	}
	
	public ArrayList<Evento> getEventoList()
	{
		ArrayList<Evento> evento = new ArrayList<Evento>();
		
		Cursor cursor = db.query("Evento", columns, null, null, null, null, null);
		
		cursor.moveToFirst();
		while(!cursor.isAfterLast())
		{
			evento.add(cursorToEvento(cursor));
			cursor.moveToNext();
		}
		
		cursor.close();
		return evento;
	}
	
		
	private Evento cursorToEvento(Cursor cursor){
		
		Evento evento = new Evento();
		evento.setId(cursor.getLong(0));
		evento.setNome(cursor.getString(1));
		evento.setDiaHora(cursor.getString(2));
		evento.setLocal(cursor.getString(3));
		evento.setDescricao(cursor.getString(4));
		
		return evento;
	}
}
